package com.onlyphoto.onlymall.mvp.view.activity

import com.onlyphoto.onlymall.R
import com.onlyphoto.onlymall.base.BaseActivity
import com.onlyphoto.onlymall.mvp.contract.CWebView
import com.onlyphoto.onlymall.mvp.presenter.PWebViewImpl
import android.content.Intent
import com.onlyphoto.onlymall.mvp.view.fragment.WebViewFragment
import com.onlyphoto.onlymall.utils.LogUtil


class WebViewActivity : BaseActivity<PWebViewImpl>(), CWebView.IVWebView {

    private var title: String? = null
    private var url: String? = null
    private var mWebViewFragment: WebViewFragment? = null
    private var isPush: Boolean = false
    private var jumpToHome: String? = null

    override fun retry() {}

    override fun loadData() {
        //加载数据
    }

    override fun init() {
        title = intent?.getStringExtra(TAG_TITLE)
        url = intent?.getStringExtra(TAG_URL)

        if (url != null && !url!!.startsWith("http")) {
            url = "http://$url"
        }

        if(url==null){
            LogUtil.log("", "url 不能为null")
            url="https://www.baidu.com"
        }

        val fragmentManager = supportFragmentManager
        if (null == mWebViewFragment) {
            val bundle = WebViewFragment.makeBundle(title, url!!, isPush, jumpToHome)
            mWebViewFragment = WebViewFragment.newInstance(bundle)
        }
        fragmentManager.beginTransaction().add(R.id.content_web, mWebViewFragment!!).commit()
    }

    override fun initListener() {
        //点击事件初始化
    }

    override fun isFullScreen(): Boolean {
        //是否沉浸式， 默认非沉浸式
        return false
    }

    override fun onBackPressed() {
        if (mWebViewFragment != null) {
            mWebViewFragment!!.onBackPressed()
        }else{
            super.onBackPressed()
        }
    }

    public override fun initCustomerStatusManager() {

    }

    override fun setContentViewId(): Int {
        return R.layout.activity_webview
    }

    override fun createPresenter() {
        mPresenter = PWebViewImpl(mContext, this)
    }

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    //QQ分享回调
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (mWebViewFragment != null) {
            mWebViewFragment!!.onActivityResult(requestCode, resultCode, data)
        }
    }

    companion object {
        const val TAG_TITLE = "title"
        const val TAG_URL = "url"
        const val TAG_AUTH_METHOD = "authMethod"
        const val TAG_IS_PUSH = "isPush"
        const val TAG_JUMP_TO_HOME = "JumpToHome"
    }
}
